package com.example.blogdemo.repository;

import com.example.blogdemo.entity.Comment;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author ：Anitaxiaofeng
 * @description：
 * @create 2017-12-18
 **/
public interface CommentRepository extends JpaRepository<Comment, Integer> {

    Comment findById(Integer id);

    Page<Comment> findAll(Pageable pageable);

    List<Comment> findCommentsByArticleId(int articleId);

    @Query(value = "select count(id) from comment where article_id=?1",nativeQuery = true)
    Integer findCommentCountByArticleId(int articleId);

    @Transactional
    @Modifying
    @Query(value = "update comment c set c.status =:status where c.id=:articleId",nativeQuery = true)
    void updateStatus(@Param("articleId")int articleId,@Param("status")int status);


    @Transactional
    @Modifying
    @Query(value = "delete from comment where id = :id",nativeQuery = true)
    public void deleteById(@Param("id") Integer id);
}



